(function () {
  layui.use("table", function () {
    var table = layui.table;
    var url = GlobalSiteUrl + "/admin/selectAllManage";
    //第一个实例
    table.render({
      elem: "#table",
      toolbar: "#toolbarDemo",
      width: "auto",
      height: "full - 100",
      url: url, //数据接口
      response: {
        statusCode: 200, //重新规定成功的状态码为 200，table 组件默认为 0
      },
      parseData: function (res) {
        if (res.data == null) {
          alert(res.msg);
          location.reload();
          return;
        } else {
          var data = res.data.list;
          data.forEach((element) => {
            element["create_time"] = formatTime(element["create_time"]);
            element["update_time"] = !element["update_time"]
              ? "暂未更新"
              : formatTime(element["update_time"]);
          });
          if (!res.data.total) {
            return {
              code: res.status,
              msg: res.msg,
              data: res.data,
              count: 1,
            };
          } else {
            return {
              code: res.status,
              msg: res.msg,
              data: res.data.list,
              count: res.data.total,
            };
          }
        }
      },
      page: true, //开启分页
      method: "post",
      id: "reloadTable",
      cols: [
        [
          {
            field: "suid",
            title: "id",
            width: 80,
            align: "center",
          },
          {
            field: "name",
            title: "用户名",
            align: "center",
            width: 220,
          },
          {
            field: "avatar",
            title: "图标",
            align: "center",
            width: 130,
            templet: "#img",
          },
          {
            field: "role_name",
            title: "角色",
            align: "center",
            width: 150,
            sort: true,
          },
          {
            field: "create_time",
            title: "创建时间",
            align: "center",
            width: 220,
            sort: true,
          },
          {
            field: "update_time",
            title: "更新时间",
            align: "center",
            width: 220,
            sort: true,
          },
          {
            fixed: "right",
            align: "center",
            title: "操作",
            toolbar: "#barDemo",
            width: 250,
          },
        ],
      ],
    });

    //监听行工具事件
    table.on("tool(table)", function (obj) {
      var data = obj.data;
      let suid = data.suid;
      if (obj.event === "del") {
        layer.confirm(
          "真的要删除 " + obj.data.name + " 管理员吗",
          {
            btn: ["确定", "取消"],
            title: "提示",
          },
          function (index) {
            var url =
              GlobalSiteUrl +
              "/admin/updateManageInfoBySuid?delState=1&suid=" +
              data.suid;
            $.post({
              url: url,
              success: function (res) {
                if (res.status == 200) {
                  layer.msg("删除成功");
                  layer.close(index);
                  setTimeout(function () {
                    location.reload();
                  }, 1000);
                } else {
                  layer.msg(res.msg, {
                    time: 1500,
                    anim: 6,
                  });
                  layer.close(index);
                }
              },
            });
            layer.close(index);
          }
        );
      } else if (obj.event === "edit") {
        layer.open({
          area: ["500px", "350px"],
          type: 1,
          title: "修改 " + obj.data.name + " 的信息（可单独修改）",
          content: $(".addPanel1"),
          btn: ["确定", "取消"],
          yes: function (index) {
            var data = $(".addForm1").serializeArray();
            console.log(data);
            let userName = data[0].value;
            let rid;
            let password;
            if (data[1].name === "rid") {
              rid = data[1].value;
              password = data[2].value;
            } else {
              password = data[1].value;
            }
            if (!userName && !rid && !password) {
              layer.msg("至少更改一个信息", {
                time: 1500,
                anim: 6,
              });
              return;
            } else {
              let data = {
                name: userName,
                rid,
                password: $.md5(password),
                suid,
              };
              var url = GlobalSiteUrl + "/admin/updateManageInfoBySuid";
              $.post({
                url: url,
                data,
                success: function (res) {
                  console.log(res);
                  if (res.status == 200) {
                    layer.msg(res.msg);
                    layer.close(index);
                    setTimeout(function () {
                      location.reload();
                    }, 1000);
                  } else {
                    layer.msg(res.msg, {
                      time: 1500,
                      anim: 6,
                    });
                    layer.close(index);
                  }
                },
              });
            }
          },
        });
      }
    });

    //按钮事件
    var $ = layui.$,
      active = {
        reload: function () {
          // 页面重载&模糊查询
          var search = $("#search");
          //执行重载
          table.reload(
            "reloadTable",
            {
              page: {
                curr: 1, //重新从第 1 页开始
              },
              where: {
                roleName: search.val(),
              },
            },
            "data"
          );
        },
      };

    // 按键监听
    $(".demoTable .layui-btn").on("click", function () {
      var type = $(this).data("type");
      active[type] ? active[type].call(this) : "";
    });

    // 新增数据
    $(".add").on("click", function () {
      var url = GlobalSiteUrl + "/admin/addManager";
      layer.open({
        area: ["550px", "320px"],
        type: 1,
        title: "新增管理员",
        content: $(".addPanel"),
        btn: ["确定", "取消"],
        yes: function (index) {
          var data = $(".addForm").serializeArray();
          console.log(data);
          let name = data[0].value;
          if (!name) {
            layer.msg("请填写用户名名称", {
              time: 1500,
              anim: 6,
            });
            return;
          } else if (data.length === 1) {
            layer.msg("请选择角色", {
              time: 1500,
              anim: 6,
            });
            return;
          } else {
            let rid = data[1].value;
            $.post({
              url: url,
              data: {
                name,
                rid,
              },
              success: function (res) {
                if (res.status == 200) {
                  layer.msg(res.msg);
                  layer.close(index);
                  setTimeout(function () {
                    location.reload();
                  }, 1000);
                } else {
                  layer.msg(res.msg, {
                    time: 1500,
                    anim: 6,
                  });
                  layer.close(index);
                }
              },
            });
          }
        },
      });
    });
  });
})();
